< previous page page_260 next page >

Page 260
If you want to know where you are in the sample company structure in Figure 12.11, you need to know more than just where the root (President) is. Now you're ready to examine the first of several tree relationships, the parent relationship. To be a parent, a node must have children. In Figure 12.11, the president of the company is a parent to both of the vice president nodes. Both managers shown in the figure also have the same parent, the VP of Finance.
Suppose you were an ambitious little node trying to climb the corporate ladder. You could use this code to check your progress:
Dim MyNode As Node

Set MyNode = tv1.Nodes("Me")

If MyNode Is MyNode.Root Then
    MsgBox "You're the boss!"
Else
    MsgBox "Try getting promoted to " & MyNode.Parent.Text
End If
As you can see from the example, each node object has a Parent property that refers to its parent node. The only exception is the root node, whose Parent property does not refer to anything (it has the special value Nothing).
Working with the Children Property
To find out whether a node is a parent, you can query the Children property by using code like this:
Private Sub TreeView1_NodeClick(ByVal MyNode As Node)
    If MyNode.Children = 0 Then
        MsgBox "I am not a parent"
    End If
End Sub
As you can see, the Children property returns an integer value that represents the number of children the given node has. To be considered a child, a node must have a direct connection. In other words, as shown in Figure 12.11, the president of the company has two childrenthe two senior vice presidents.
Working with the Child property
Whereas the Children property simply returns the count of child nodes, the Child property returns an actual node (just like the Root and Parent properties). However, even if a parent has multiple children, the Child property returns only the first of the given parent node's descendants. If a given node doesn't have a child, then its Child property contains the value Nothing. Therefore, you should not try to access it if the Children property is 0. In the example shown in Figure 12.11, the second manager's Child property refers to the accountant, Walter Mitty.
The immediate problem that comes to mind is how to access child nodes other than the first one. Each node has several properties that are used to determine who its "brothers and sisters" are, as demonstrated in the following code:
Sub PrintAllChildren()

   Dim anyNode As Node
   Dim kidNode As Node

 
< previous page page_260 next page >